/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.lundberg.podcasts.types;

import java.text.ParseException;
import java.util.Date;

import org.lundberg.podcasts.util.Util;

import android.database.Cursor;
import android.os.Parcel;
import android.os.Parcelable;

/**
 *
 * @author lundbergg
 */
public class Channel implements Parcelable, Comparable<Channel>
{
	// These variables are stored in the channels table
    public int id;
    public String title;
    public String author;
    public String url;
    public String imageURL;
    // ISO8601 format
    private String pubDate;
    
    // These variables are set dynamically at runtime
    public int new_items = 0;
    

    public Channel() {
    }
    
    
    public Channel(Cursor cursor)
    {
    	int index=-1;
        
        if ((index = cursor.getColumnIndex("_id")) > -1) {
        
        	this.id = cursor.getInt(index);
        }
        else if ((index = cursor.getColumnIndex("id")) > -1) {
        	
        	this.id = cursor.getInt(index);
        }
        
        if ((index = cursor.getColumnIndex("title")) > -1) {
            
        	this.title = cursor.getString(index);
        }
        
        if ((index = cursor.getColumnIndex("author")) > -1) {
            
        	this.author = cursor.getString(index);
        }
        
		if ((index = cursor.getColumnIndex("url")) > -1) {
				            
			this.url = cursor.getString(index);
        }
		
		if ((index = cursor.getColumnIndex("imageURL")) > -1) {
		    
			this.imageURL = cursor.getString(index);
		}
		
		if ((index = cursor.getColumnIndex("pubDate")) > -1) {
		    
			this.pubDate = cursor.getString(index);
		}
		
		if ((index = cursor.getColumnIndex("new_items")) > -1) {
		    
			this.new_items = cursor.getInt(index);
		}
    }
    
    
    public Channel(int id, String title, String author, String url, String imageURL, String pubDate)
    {
    	this.id = id;
        this.title = title;
        this.author = author;
        this.url = url;
        this.imageURL = imageURL;
        this.pubDate = pubDate;
    }
    
    public Channel(Parcel in)
    {
    	this.id = in.readInt();
    	this.title = in.readString();
    	this.author = in.readString();
    	this.url = in.readString();
    	this.imageURL = in.readString();
    	this.pubDate = in.readString();
    	this.new_items = in.readInt();
    }
    
    
    public void setPubDate(String pubDate)
    {
    	if (pubDate != null)
    	{
	    	try
	        {
	        	// ISO8601
	        	if (Character.isDigit(pubDate.charAt(0)))
	        	{
	        		this.pubDate = pubDate;
	        	}
	        	// RFC 822
	        	else if (Character.isDigit(pubDate.charAt(pubDate.length()-1)))
	        	{
	        		Date rfc822Date = Util.RFC822_DATE_FORMAT_1.parse(pubDate);
	            	this.pubDate = Util.ISO8601_DATE_FORMAT.format(rfc822Date);
	        	}
	        	else
	        	{
	        		Date rfc822Date = Util.RFC822_DATE_FORMAT_2.parse(pubDate);
	            	this.pubDate = Util.ISO8601_DATE_FORMAT.format(rfc822Date);
	        	}
	        }
	        catch(ParseException e) {
	            e.printStackTrace();
	        }
    	}
    }
    
    
    public String getPubDate() {
    	
    	return this.pubDate;
    }

	@Override
	public int compareTo(Channel arg0) {
		
		return this.title.compareTo(arg0.title);
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Channel other = (Channel) obj;
		if (id != other.id)
			return false;
		return true;
	}
	
	
	
	@Override
	public int describeContents() {
		return 0;
	}


	@Override
	public void writeToParcel(Parcel out, int flags) {
		
		out.writeInt(id);
		out.writeString(title);
		out.writeString(author);
		out.writeString(url);
		out.writeString(imageURL);
		out.writeString(pubDate);
		out.writeInt(new_items);
	}
    
	
	
	/**
    * This field is needed for Android to be able to
    * create new objects, individually or as arrays.
    */
   public static final Parcelable.Creator<Channel> CREATOR =
		   new Parcelable.Creator<Channel>() {
	           public Channel createFromParcel(Parcel in) {
	               return new Channel(in);
	           }
	           public Channel[] newArray(int size) {
	               return new Channel[size];
	           }
   };

}
